/**************************************************
 * Project Name         [JavaEE_XML]
 * File Name            [StudentDAO.java]
 * Creation Date        [08-15-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 **************************************************/
package pkg_exam.dao;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import pkg_exam.bean.Student;
import pkg_exam.exception.StudentNotFoundExcepiton;
import pkg_exam.util.StudentUtil;

/**
 * StudentDAO: add, delete, update and search information
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class StudentDAO {

	/**
	 * Add new student
	 * 
	 * @param student
	 */
	public void add(Student student) {
		try {
			Document document = StudentUtil.getDocument();

			Element studentElement = document.createElement("student");
			studentElement.setAttribute("studentId", student.getId());

			Element nameElement = document.createElement("name");
			nameElement.setTextContent(student.getName());
			studentElement.appendChild(nameElement);

			Element examIdElement = document.createElement("examID");
			examIdElement.setTextContent(student.getExamId());
			studentElement.appendChild(examIdElement);

			Element locationElement = document.createElement("location");
			locationElement.setTextContent(student.getLocation());
			studentElement.appendChild(locationElement);

			Element scoreElement = document.createElement("score");
			scoreElement.setTextContent(String.valueOf(student.getScore()));
			studentElement.appendChild(scoreElement);

			document.getElementsByTagName("exam").item(0)
					.appendChild(studentElement);

			StudentUtil.writeToXML(document);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

	/**
	 * Query student by ID
	 * 
	 * @param id
	 * @return
	 */
	public Student searchById(String id) {
		Student student = null;
		try {
			Document document = StudentUtil.getDocument();

			NodeList studentNodes = document.getElementsByTagName("student");
			for (int i = 0; i < studentNodes.getLength(); i++) {
				Element studentNode = (Element) studentNodes.item(i);
				String idFromDocuement = studentNode.getAttribute("studentId");
				if (idFromDocuement.equals(id)) {
					student = new Student();
					student.setId(id);
					student.setName(studentNode.getElementsByTagName("name")
							.item(0).getTextContent());
					student.setExamId(studentNode
							.getElementsByTagName("examID").item(0)
							.getTextContent());
					student.setLocation(studentNode
							.getElementsByTagName("location").item(0)
							.getTextContent());
					student.setScore(Double.valueOf(studentNode
							.getElementsByTagName("score").item(0)
							.getTextContent()));
					break;
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return student;
	}

	/**
	 * Remove student by name
	 * 
	 * @param name
	 * @throws StudentNotFoundExcepiton
	 */
	public void deleteByName(String name) throws StudentNotFoundExcepiton {
		try {
			Document document = StudentUtil.getDocument();

			NodeList studentNodes = document.getElementsByTagName("student");
			for (int i = 0; i < studentNodes.getLength(); i++) {
				Element studentNode = (Element) studentNodes.item(i);
				String nameFromDocument = studentNode
						.getElementsByTagName("name").item(0).getTextContent();
				if (nameFromDocument.equals(name)) {
					studentNode.getParentNode().removeChild(studentNode);
					StudentUtil.writeToXML(document);
					return;
				}
			}

			throw new StudentNotFoundExcepiton(
					"Can not find student with name " + name);
		} catch (StudentNotFoundExcepiton e) {
			throw e;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
